package view;

import java.awt.Color;
import java.awt.GridLayout;
import java.util.*;
import javax.swing.*;

import vossenenkonijnen.Actor;

import controller.Field;
import controller.FieldStats;

/**
 * A textview of the simulation, displays the stats in text
 * 
 * @author Projectgroep 5 
 */

public  class TextView extends JPanel implements SimulatorView
{
	// instance variables.
    private FieldStats stats;
    private ArrayList<JLabel> labelList;

    /**
     * Constructor for objects of class TextView
     */
    public TextView()
    {
    	stats = new FieldStats();
        labelList = new ArrayList<JLabel>();
        
        this.setLayout(new GridLayout(0,1));
    }
    
    /**
     * Create the text for the textview
     * @param s the string to add to textview
     */
    public void createText(String s) {
    	this.removeAll();
    	JLabel label = new JLabel(s);
    	if(labelList.size()>60) {
    		labelList.remove(0);
    	}
    	labelList.add(label);
    	for(int i = 0; i<labelList.size(); i++) {
    		this.add(labelList.get(i));
    	}
    }
    
    @Override
    public void showStatus(int step, Field field)
    {
    	stats.reset();
        for(int row = 0; row < field.getDepth(); row++) {
            for(int col = 0; col < field.getWidth(); col++) {
                Actor actor = (Actor)field.getObjectAt(row, col);
                if(actor != null) {
                    stats.incrementCount(actor.getClass());
                }
            }
        }
        stats.countFinished();
        
        String x = "Step " + step + ", population: " + stats.getPopulationDetails(field);
        createText(x);
    }
    
    @Override
    public boolean isViable(Field field)
    {
        return stats.isViable(field);
    }
    
    @Override
    public void setColor(Class animalClass, Color color)
    {
        // Do nothing, textview doesn't need color
    }
}